package com.qst.action.PersonalityDimension;

import com.qst.BaseServlet;
import com.qst.RequestUtil;
import com.qst.WebUtil;
import com.qst.entity.AssessmentType;
import com.qst.entity.PersonalityDimension;
import com.qst.service.AssessmentService;
import com.qst.service.DimensionService;
import com.qst.service.impl.ServiceFactory;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

//@WebServlet("/dimension/list.action")
public class ListServlet extends BaseServlet {

    private AssessmentService assessmentService = ServiceFactory
            .getService(AssessmentService.class);
    private DimensionService dimensionService = ServiceFactory
            .getService(DimensionService.class);


    @Override
    protected void doAction(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String submitButton = RequestUtil.getString(req, "create");
        if (!submitButton.isEmpty())// 添加操作
        {
            doCreate(req, resp);
        } else {
            doList(req, resp);
        }
    }

    private void doList(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setAttribute("assessmentList", assessmentService.findAllAssessment());
        int id = RequestUtil.getInt(req, "id");
        List<PersonalityDimension> dimensionList = dimensionService.findDimensionByAssessment(id);
        req.setAttribute("dimensionList", dimensionList);
        WebUtil.forward(req, resp, "/dimension/list.jsp");
    }

    private void doCreate(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int assessmentId = RequestUtil.getInt(req, "id");
        AssessmentType assessment = assessmentService.findAssessmentById(assessmentId);
        req.setAttribute("assessment", assessment);
        WebUtil.forward(req, resp, "/dimension/create.jsp");
    }
}
